#!/bin/bash
yum install -y expect
SERVERS="178.119.112.121 178.119.112.147 178.119.112.98 178.119.112.77 178.119.112.254 178.119.112.223 178.119.112.141 178.119.112.124 178.119.112.249"
PASSWORD=Huayun@123
auto_ssh_copy_id() {
    expect -c "set timeout -1;
        spawn ssh-copy-id $1;
        expect {
            *(yes/no)* {send -- yes\r;exp_continue;}
            *assword:* {send -- $2\r;exp_continue;}
            eof        {exit 0;}
        }";
}

ssh_copy_id_to_all() {
    for SERVER in $SERVERS
    do
        sed -i "/\[storage]/a\\${SERVER}" hosts
        auto_ssh_copy_id $SERVER $PASSWORD
    done
}
ssh_copy_id_to_all
